package com.three.lab.page.message;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.material.snackbar.Snackbar;
import com.three.lab.R;
import com.three.lab.adapter.OrderMsgAdapter;
import com.three.lab.base.AppConstant;
import com.three.lab.base.BaseActivity;
import com.three.lab.bean.OrderMsgBean;
import com.three.lab.util.XpopupUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单消息
 * created by lqd on 2024-07-01.
 */
public class OrderMsgActivity extends BaseActivity {

    private TextView tvTitle, tvLoading;
    private View mLoadingView;
    private ImageView ivState;
    private ProgressBar progressBar;
    private SwipeRefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private OrderMsgAdapter adapter;

    private int pageNo = 1;
    private boolean isloadMore = false;

    @Override
    protected void initParms(Bundle bundle) {}

    @Override
    protected int bindLayout() {
        return R.layout.common_refresh_list_layout;
    }

    @Override
    protected void initViews(View view, Bundle savedInstanceState) {
        tvTitle = $(R.id.tv_title);
        tvTitle.setText(getString(R.string.order_msg));

        refreshLayout = $(R.id.refresh_layout);
        mLoadingView = LayoutInflater.from(this).inflate(R.layout.loading_view, null);
        ivState = mLoadingView.findViewById(R.id.iv_state);
        tvLoading = mLoadingView.findViewById(R.id.tv_loading);
        progressBar = mLoadingView.findViewById(R.id.progressBar);

        recyclerView = $(R.id.recycleview);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new OrderMsgAdapter();
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void setListener() {
        $(R.id.iv_left).setOnClickListener(this);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onRefreshing();
            }
        });
        tvLoading.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRefreshing();
            }
        });
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                recyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (isloadMore) {
                            pageNo++;
                            loadData();
                        }
                    }
                }, 1000);
            }
        }, recyclerView);
        adapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                XpopupUtil.showOnce(OrderMsgActivity.this, getString(R.string.tips_delete), new XpopupUtil.OnPopupOkListener() {
                    @Override
                    public void onPopupConfirm() {
//                        OrderMsgBean bean = (OrderMsgBean) baseQuickAdapter.getItem(position);
                        showLoading(getString(R.string.deleting));
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                hideLoading();
                                baseQuickAdapter.remove(position);
                                baseQuickAdapter.notifyDataSetChanged();
                                Snackbar.make(view, getString(R.string.delete_success), Snackbar.LENGTH_LONG).show();
                            }
                        },500);

                    }
                });
                return true;
            }
        });
    }

    @Override
    protected void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.iv_left:
                finish();
                break;
        }
    }

    @Override
    protected void initData(Context mContext) {
        onRefreshing();
    }

    private void onRefreshing() {
        progressBar.setVisibility(View.VISIBLE);
        tvLoading.setText(getString(R.string.loading));
        ivState.setVisibility(View.GONE);
        adapter.setEnableLoadMore(true);
        adapter.getData().clear();
        if (refreshLayout != null && refreshLayout.isRefreshing()) {
            refreshLayout.setRefreshing(true);
        }
        pageNo = 1;
        isloadMore = false;
        adapter.notifyDataSetChanged();
        loadData();
    }

    private void loadData() {
        adapter.setEmptyView(mLoadingView);

        List<OrderMsgBean> mlist = new ArrayList<>();
        for (int i = 0; i < 37; i++) {
            boolean isDef = false;
            if (i == 0) {
                isDef = true;
            }
            OrderMsgBean bean = new OrderMsgBean(i, "您的后东方消毒液 已由【客户指定地点】代收", "2个月前", "广东省广州市天河区");
            mlist.add(bean);
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // List<AddressListBean> tempList = GsonUtils.parseJsonArrayWithGson(result,AddressListBean.class);
                onResetRefresh();
                setListAdapter(mlist);//设置页面数据
            }
        }, 1000);
    }

    private void setListAdapter(List<OrderMsgBean> mlist) {
        if (isloadMore) {
            adapter.addData(mlist);
            if (mlist.size() < AppConstant.DEFAULT_PAGE_SIZE) {
                adapter.loadMoreEnd();
            } else {
                adapter.loadMoreComplete();
                adapter.setEnableLoadMore(true);
            }
        } else if (mlist.isEmpty()) {
            adapter.loadMoreEnd();
            adapter.setEnableLoadMore(false);
            setState(getString(R.string.no_data), R.mipmap.ic_empty);
        } else {
            adapter.setNewData(mlist);
            adapter.loadMoreComplete();
            adapter.setEnableLoadMore(true);

            isloadMore = false;
            adapter.loadMoreEnd();
        }
    }

    private void setState(String string, int resId) {
        progressBar.setVisibility(View.GONE);
        tvLoading.setText(string);
        ivState.setImageResource(resId);
        ivState.setVisibility(View.VISIBLE);
        adapter.setEmptyView(mLoadingView);
        adapter.notifyDataSetChanged();
    }

    private void onResetRefresh() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.setRefreshing(false);
        }
        progressBar.setVisibility(View.GONE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        onResetRefresh();
    }
}